/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.upc.sigu.modelo.daoImpl;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import pe.edu.upc.sigu.entities.Alumnos;
import pe.edu.upc.sigu.modelo.dao.AlumnoDao;

/**
 *
 * @author Usuario2
 */

@Repository(value ="alumnoDao")
public class AlumnoDaoImpl implements AlumnoDao {



    private  EntityManager entityManager =null;
    

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager){
        this.entityManager = entityManager;
    }

    @Override
    //@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    public List getAlumnosList() {
        String sql = "SELECT a FROM Alumnos a";
         Query query = entityManager.createQuery(sql);
        return query.getResultList();
    }

    @Override
    public Alumnos getAlumnosList(Alumnos alumnos) {
       String sql = "SELECT a FROM Alumnos a "
               + " WHERE a.alumnosPK.alumno=:alumno "
               + " AND  a.alumnosPK.institucion=:institucion ";
         Query query = entityManager.createQuery(sql);
         query.setParameter("alumno", alumnos.getAlumnosPK().getAlumno());
         query.setParameter("institucion", alumnos.getAlumnosPK().getInstitucion());
         List resulList = query.getResultList();
         if(resulList.size()>0){
            Alumnos alumno  = (Alumnos) resulList.get(0);
            return alumno;
         }
        return null;
    }


    /**
     * @return the entityManager
     */
    public EntityManager getEntityManager() {
        return entityManager;
    }



}
